//
//  MusicLrcsScrollerView.m
//  MyMusic
//
//  Created by ioser on 17/5/6.
//  Copyright © 2017年 ioser. All rights reserved.
//

#import "MusicLrcsScrollerView.h"
#import "Masonry.h"
#import "LrcsTableViewCell.h"
#import "LrcsTool.h"
#import "LrcsModel.h"


@interface MusicLrcsScrollerView () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic)UITableView *tableView;
@property (strong, nonatomic)NSArray *lrcArray;
@property (assign, nonatomic)NSInteger currentIndext;

@end


@implementation MusicLrcsScrollerView

- (NSArray *)lrcArray
{
    if (_lrcArray == nil) {
        _lrcArray = [NSArray array];
        
    }
    return _lrcArray;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.contentSize = CGSizeMake(ScreenW * 2, 0);
    self.backgroundColor = [UIColor clearColor];
    self.showsHorizontalScrollIndicator = NO;
    self.pagingEnabled = YES;
    self.bounces = NO;
    
    UITableView *tableview = [[UITableView alloc] init];
    tableview.bounces = NO;
    self.tableView = tableview;
    [self addSubview:tableview];
    
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
//    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom);
        make.height.equalTo(self.mas_height);
        make.right.equalTo(self.mas_right);
        make.left.equalTo(self.mas_left).offset(self.bounds.size.width);
        make.width.equalTo(self.mas_width);
    }];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
//    self.tableView.contentInset = UIEdgeInsetsMake(self.tableView.bounds.size.height / 2, 0, 0, 0);
    self.tableView.contentInset = UIEdgeInsetsMake(self.bounds.size.height / 2, 0, 0, 0);

    [super layoutIfNeeded];
}

#pragma mark - Delegate DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.lrcArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LrcsTableViewCell *cell = [LrcsTableViewCell cellWithTableView:tableView];
    LrcsModel *model = self.lrcArray[indexPath.row];
    cell.lrcsLabel.text = [NSString stringWithFormat:@"%@",model.text];
    if (indexPath.row == self.currentIndext) {
        cell.lrcsLabel.font = [UIFont systemFontOfSize:17];
    } else {
        cell.lrcsLabel.font = [UIFont systemFontOfSize:14];
        cell.lrcsLabel.progress = 0;
    }
    return cell;
}


// 处理歌词

- (void)setLrcFileName:(NSString *)LrcFileName
{
    // -1让tableView滚到中间
    [self.tableView setContentOffset:CGPointMake(0, -self.tableView.bounds.size.height * 0.5) animated:NO];
    
    // 0.将currentIndex设置为0
    self.currentIndext = 0;
    _LrcFileName = [LrcFileName copy];
    
    // 把歌词名传到工具类里面
    self.lrcArray = [LrcsTool GetlrcLine:_LrcFileName];
    
    [self.tableView reloadData];
}


// 外面displayerLink 触发这个方法  一秒钟调用60次
- (void)setCurrentTime:(NSTimeInterval)currentTime
{
    // 当前播放器播放的时间
    _currentTime = currentTime;
    
    for (NSInteger indext = 0; indext < self.lrcArray.count; indext++) {
        // 当前行歌词 数据模型
        LrcsModel *currentModel = self.lrcArray[indext];
        // 下一行歌词 数据模型
        LrcsModel *nextModel = nil;
        NSInteger nextIndext = indext + 1;
        if (nextIndext < self.lrcArray.count) {
            nextModel = self.lrcArray[nextIndext];
        }
        
        if (self.currentTime != indext && currentTime >= currentModel.time && currentTime < nextModel.time) {
            
            NSIndexPath *indextpath = [NSIndexPath indexPathForRow:indext inSection:0];
            
            NSIndexPath *lastIndextpath = [NSIndexPath indexPathForRow:self.currentIndext inSection:0];
            
            self.currentIndext = indext;
            
            [self.tableView reloadRowsAtIndexPaths:@[lastIndextpath] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableView reloadRowsAtIndexPaths:@[indextpath] withRowAnimation:UITableViewRowAnimationNone];
            
            [self.tableView scrollToRowAtIndexPath:indextpath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        }
        
        // 当前歌词
        if (self.currentIndext == indext) {
            CGFloat value = (currentTime - currentModel.time) / (nextModel.time - currentModel.time);
            NSIndexPath *indextpath = [NSIndexPath indexPathForRow:indext inSection:0];
            LrcsTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indextpath];
            value += 0.1;
            cell.lrcsLabel.progress = value;
            
            // 把当前的显示歌词回调到控制器里面
            !self.lrcBlock ? : self.lrcBlock(value,currentModel.text);
        }
    }
}



@end
